﻿using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Linq;
using UnityEngine;

public class Director : MonoBehaviour
{
    // 主屏幕
    public GameObject screenMain;
    // 附屏幕
    public GameObject screenAttached;
    // 当前分数
    public GameObject score;
    // 最高分数
    public GameObject scoreHigh;
    // 关卡
    public GameObject level;
    // 暂停提示
    public GameObject pauseHint;
    // Start is called before the first frame update
    // 默认图层  存放所有固定的块
    Layer layerDefault;
    // 块的图层  存放活动的块
    Layer layerT = null;
    List<T_EnumType> num = new List<T_EnumType>() {
        T_EnumType.TYPE_O,
        T_EnumType.TYPE_I,
        // T_EnumType.TYPE_I,
        // T_EnumType.TYPE_I,
        // T_EnumType.TYPE_I,
        T_EnumType.TYPE_J,
        T_EnumType.TYPE_L,
        T_EnumType.TYPE_S,
        T_EnumType.TYPE_Z,
        T_EnumType.TYPE_T,
    };
    private float _time = 1;
    public float time {
        get {
            return _time;
        }
        set {
            if (value >= 2 || value <= 0) {
                Debug.Log("超出速度了");
            } else {
                CancelInvoke();
                _time = value;
                InvokeRepeating("gameStarting", time, time);
            }
        } 
    }
    void Start()
    {
        ScreenMain sm = screenMain.GetComponent<ScreenMain>();
        sm.GetComponent<ScreenMain>().init();
        layerDefault = sm.getLayer("layerDefault");
        // sm.addLayer("layerT");
        // Layer layerT = sm.getLayer("layerT");
        // if (layerT != null) {
        //     layerT.viewData.AddRange(getT());
        // }
        
        InvokeRepeating("gameStarting", 0, time);
    }
    void gameStarting() {

        ScreenMain sm = screenMain.GetComponent<ScreenMain>();
        layerT = sm.getLayer("layerT");
        if (layerT == null) {
            sm.addLayer("layerT");
            layerT = sm.getLayer("layerT");
            layerT.point = new MyPoint(Mathf.RoundToInt(sm._screenW / 2), sm._screenH);
            layerT.viewData.AddRange(getT());
        }
        Debug.Log(time);
        


        MyPoint point = layerT.point;
        point._y -= 1;
        layerT.point = point;
        // List<MyPoint> listNew = layerDefault.viewData.Union(temp).ToList<MyPoint>();
        sm.refScreenMain();
    }
    private void Update() {
        if (Input.GetKeyDown(KeyCode.RightBracket)) {
            time -= 0.1f;
        } else if (Input.GetKeyDown(KeyCode.LeftBracket)) {
            time += 0.1f;
        } else if (Input.GetKeyDown(KeyCode.W)) {
            // List<MyPoint> temp = new List<MyPoint>();
            // foreach (var item in layerT.viewData)
            // {
            //     MyPoint point = new MyPoint(item._x, item._y + 1);
            //     temp.Add(point); 
            // }
            // layerT.viewData = temp;
        }  else if (Input.GetKeyDown(KeyCode.S)) {
            List<MyPoint> temp = new List<MyPoint>();
            foreach (var item in layerT.viewData)
            {
                MyPoint point = new MyPoint(item._x, item._y - 1);
                temp.Add(point); 
            }
            layerT.viewData = temp;
            // gameStarting();
            screenMain.GetComponent<ScreenMain>().refScreenMain();
        } else if (Input.GetKeyDown(KeyCode.A)) {
            List<MyPoint> temp = new List<MyPoint>();
            foreach (var item in layerT.viewData)
            {
                if (item._x + layerT.point._x <= 0) return;
                // 判断左边是否有格挡
                if (layerDefault.viewData.Contains(new MyPoint(item._x - 1 + layerT.point._x, item._y + layerT.point._y))) return;

                MyPoint point = new MyPoint(item._x - 1, item._y);
                temp.Add(point);
            }
            layerT.viewData = temp;
            screenMain.GetComponent<ScreenMain>().refScreenMain();;
        } else if (Input.GetKeyDown(KeyCode.D)) {
            List<MyPoint> temp = new List<MyPoint>();
            foreach (var item in layerT.viewData)
            {

                // 判断左边是否有格挡
                if (item._x + layerT.point._x >= 9) return;
                if (layerDefault.viewData.Contains(new MyPoint(item._x - 1 + layerT.point._x, item._y + layerT.point._y))) return;
                MyPoint point = new MyPoint(item._x + 1, item._y);
                temp.Add(point); 
            }
            layerT.viewData = temp;
            screenMain.GetComponent<ScreenMain>().refScreenMain();;
        }
    }
    // Update is called once per frame
    // 创建各种方块
    List<MyPoint> createT(T_EnumType enumType) {
        List<MyPoint> list = new List<MyPoint>();
        switch (enumType)
        {
            case T_EnumType.TYPE_init:
                list.Add(new MyPoint(0, 0));
                break;
            case T_EnumType.TYPE_O:
                list.Add(new MyPoint(0, 0));
                list.Add(new MyPoint(0, 1));
                list.Add(new MyPoint(1, 0));
                list.Add(new MyPoint(1, 1));
                break;
            case T_EnumType.TYPE_I:
                list.Add(new MyPoint(0, 0));
                list.Add(new MyPoint(0, 1));
                list.Add(new MyPoint(0, 2));
                list.Add(new MyPoint(0, 3));
                break;
            case T_EnumType.TYPE_J:
                list.Add(new MyPoint(0, 0));
                list.Add(new MyPoint(1, 0));
                list.Add(new MyPoint(1, 1));
                list.Add(new MyPoint(1, 2));
                break;
            case T_EnumType.TYPE_L:
                list.Add(new MyPoint(0, 0));
                list.Add(new MyPoint(0, 1));
                list.Add(new MyPoint(0, 2));
                list.Add(new MyPoint(1, 0));
                break;
            case T_EnumType.TYPE_S:
                list.Add(new MyPoint(0, 0));
                list.Add(new MyPoint(1, 0));
                list.Add(new MyPoint(1, 1));
                list.Add(new MyPoint(2, 1));
                break;
            case T_EnumType.TYPE_Z:
                list.Add(new MyPoint(0, 1));
                list.Add(new MyPoint(1, 0));
                list.Add(new MyPoint(1, 1));
                list.Add(new MyPoint(2, 0));
                break;
            case T_EnumType.TYPE_T:
                list.Add(new MyPoint(0, 1));
                list.Add(new MyPoint(1, 0));
                list.Add(new MyPoint(1, 1));
                list.Add(new MyPoint(2, 1));
                break;
            case T_EnumType.TYPE_test:
                list.Add(new MyPoint(0, 0));
                break;

            default:
            break;
        }
        return list;
    }
    // 随机生成一个可控概率的方块
    List<MyPoint> getT() {
        Debug.Log(num.Count);
        int i = Random.Range(0, num.Count);
        return createT(num[i]);
    }
}
